package com.yaorange.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.api.Result;
import com.yaorange.entity.Article;
import com.yaorange.entity.ArticleType;
import com.yaorange.mapper.ApplyMapper;
import com.yaorange.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: 啊啊啊林
 * @version: 1.0
 * @create: 2021-09-23 14:11:53
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Resource
    private ApplyMapper applyMapper;
    @GetMapping("/all/{pageNum}/{pageSize}")
    public Result<?> getAllArticle(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {

        Result<PageInfo<Article>> result = new Result<>();
        PageInfo<Article> pageInfo = articleService.findAllByPage(pageNum, pageSize);
        result.setData(pageInfo);
        result.setSuccess(true);

        return result;
    }


    @GetMapping("/ByCondition")
    public Result<?> getAllArticleByCondition(String goodsName, Integer typeId) {
        System.out.println(goodsName);
        System.out.println(typeId);

        Result<PageInfo<Article>> result = new Result<>();
        PageInfo<Article> pageInfo = articleService.selectByConditonArticle(goodsName, typeId);
        System.out.println(pageInfo.getList());

        result.setSuccess(true);
        result.setData(pageInfo);
        return result;
    }

    @GetMapping("/{articleId}")
    public Result<?> deleteByArticleId(@PathVariable Integer articleId) {
        Result<Object> result = new Result<>();
        int i = articleService.deleteByArticleId(articleId);
        result.setSuccess(true);
        return result;
    }

    //弹出窗口数据回显
    @GetMapping("/windowData/{articleId}")
    public Result<?> windowDataGet(@PathVariable Integer articleId) {
        Result<Article> articleResult = new Result<>();
        Article article = articleService.selectWindowData(articleId);
        articleResult.setData(article);
        articleResult.setSuccess(true);
        return articleResult;
    }

    @GetMapping("/updateArticle")
    public Result<?> updateArticleByArticleId(Integer articleId, String articleName,
                                           Integer stockNum, String stockUnit, Integer articleType) {
        String unitName="";
        Article article = new Article();
        article.setArticleId(articleId);
        article.setArticleName(articleName);
        article.setStockNum(stockNum);
        if (stockUnit != null) {
            switch (Integer.valueOf(stockUnit)) {
                case 1:unitName="个";
                    break;
                case 2:unitName="个";
                    break;
                case 3:
                    unitName="本";
                    break;
                case 4:
                    unitName="本";
                    break;
                case 5:
                    unitName="盒";
                    break;
                case 6:
                    unitName="盒";
                    break;
                case 7:
                    unitName="包";
                    break;
                    default:
                        unitName="无";
                        break;

            }
        }
        article.setStockUnit(unitName);

        article.setArticleType(articleType);
        System.out.println(article);

        int i = articleService.updateArticle(article);
        Result<Object> result = new Result<>();
        result.setSuccess(true);
        return result;
    }

    @DeleteMapping("/deleteAllByChecked")
    public Result<?> deleteAllByChecked(@RequestBody  Integer str[]) {
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
        }
      int i=  articleService.deleteMany(str);
        Result<Object> result = new Result<>();
        result.setSuccess(true);
        result.setMessage("删除成功");

        return result;
    }

    @PostMapping("/inserNewArticle")
    public Result<?> insertNewArticle(@RequestBody Article article) {
        System.out.println(article);
        Result<Object> result = new Result<>();
       int i= articleService.insertNewArticle(article);
        if (i > 0) {
            result.setMessage("插入成功");
            result.setSuccess(true);
            return result;
        }


        return result;
    }

    @PostMapping("/zTime")
    public void testzzzz( @RequestBody String date) {

        applyMapper.insertStringTime(date);

    }

    @PostMapping("/testUser")
    public void testaR(@RequestBody ArticleType articleType) {
        System.out.println(articleType);

    }

}
